package com.ruoyi.web.controller.zhuangxiucontroller.shopController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.WithdrawDeposit;
import com.ruoyi.system.domain.supe.PlatformCoupon;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.mapper.zhuangxiumapper.shopMapper.SupplierInfoMapper;
import com.ruoyi.system.service.IWithdrawDepositService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.PlatformCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.web.PortResolverImpl;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/shopping/supplier")
public class SupplierInfoController extends BaseController {


    @Autowired(required = false)
    private SupplierInfoMapper supplierInfoMapper;

    @Autowired(required = false)
    private TokenService tokenService;

    @Autowired(required = false)
    private PlatformCouponService platformCouponService;
    @Autowired
    private IWithdrawDepositService withdrawDepositService;

    @PreAuthorize("@ss.hasPermi('shopping:supplier:findAllList')")
    @GetMapping("/findAllList")
    public List<PlatformCoupon> findAllList(){
        return platformCouponService.findAllList();
    }


    @PreAuthorize("@ss.hasPermi('shopping:supplier:listsi')")
    @GetMapping("/list")
    public TableDataInfo selectSupplierInfoList(SupplierInfo supplierInfo){
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        supplierInfo.setUserId(userId);
        supplierInfo.setSupplierTypeform(1);
        supplierInfo.setSupplierType(1);
//        System.err.println("**************************************************************************");
        List<SupplierInfo> supplierInfos = supplierInfoMapper.selectSupplierInfoList(supplierInfo);
        return getDataTable(supplierInfos);
    }



    @PreAuthorize("@ss.hasPermi('shopping:supplier:querysi')")
    @GetMapping(value = "/getInfo/{supplierId}")
    public AjaxResult selectSupplierInfoById(@PathVariable("supplierId") Integer supplierId){
        return AjaxResult.success(supplierInfoMapper.selectSupplierInfoById(supplierId));
    }

    @PreAuthorize("@ss.hasPermi('shopping:supplier:addsi')")
    @Log(title = "供应商信息添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addsi(@RequestBody SupplierInfo supplierInfo){
//        return toAjax(supplierInfoMapper.addsi(supplierInfo));
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        supplierInfo.setCustomerId(userId);
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        System.err.println("=========="+num);

        WithdrawDeposit wd = new WithdrawDeposit();


        supplierInfo.setSupplierCode(num);
        supplierInfo.setSupplierType(1);  // 供应商类型：1.自营，2.平台
        supplierInfo.setSupplierTypeform(1); //店铺类型：1.物品，2.技术
        supplierInfo.setSupplierStatus("0");  //1禁止，0启用
        supplierInfo.setModifiedTime(new Date());
        wd.setCode(num);
        wd.setCustomerId(userId);
        withdrawDepositService.insertWithdrawDeposit(wd);    //添加一张资金记录表
        supplierInfoMapper.addsi(supplierInfo);
        return AjaxResult.success("操作成功");
    }



    @PreAuthorize("@ss.hasPermi('shopping:supplier:editsi')")
    @Log(title = "供应商信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateSupplierInfo(@RequestBody SupplierInfo supplierInfo){
        return toAjax(supplierInfoMapper.updateSupplierInfo(supplierInfo));
    }

    /** 此处是修改服模块的 */
    @PutMapping(value = "/fwedit")
    public AjaxResult fwedit(@RequestBody SupplierInfo supplierInfo){
        return toAjax(supplierInfoMapper.updateSupplierInfo(supplierInfo));
    }

    @GetMapping(value = "/fwgetInfo/{supplierId}")
    public AjaxResult fwgetInfo(@PathVariable("supplierId") Integer supplierId){
        return AjaxResult.success(supplierInfoMapper.selectSupplierInfoById(supplierId));
    }

    @PreAuthorize("@ss.hasPermi('shopping:supplier:removesi')")
    @Log(title = "供应商信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierId}")
    public AjaxResult deleteSupplierInfoByIds(@PathVariable Integer[] supplierId){
        return toAjax(supplierInfoMapper.deleteSupplierInfoByIds(supplierId));
    }


    //状态修改
    @PreAuthorize("@ss.hasPermi('shopping:supplier:editStatus')")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult changeStatus(@RequestBody SupplierInfo supplierInfo){
        return toAjax(supplierInfoMapper.changeStatus(supplierInfo));
    }



}
